package cn.ffcs.uec.outStaffDataModificationApply.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Blob;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Hibernate;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.image.Image;
import org.zkoss.util.media.Media;
import org.zkoss.web.servlet.Servlets;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.PictureTable;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaffDataModificationApply.action.bean.UecOutDocEduApprovalViewMainBean;
import cn.ffcs.uec.recordsApproval.constant.ApprovalConstants;
import cn.ffcs.uec.recordsApproval.manager.ActRunTaskManager;
import cn.ffcs.uec.recordsApproval.util.ApprovalUtil;
import cn.ffcs.uec.staff.manager.UecDocEduManager;
import cn.ffcs.uec.staff.model.UecDocEdu;
import cn.ffcs.uec.staffRecords.vo.AtiOaBaseObject;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.model.DefaultDaoFactory;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

/**
 * 外包人员教育经历审批弹框Composer
 * 
 * @author zhanglu
 *
 */
@Controller
@Scope("prototype")
public class UecOutDocEduApprovalViewMainComposer extends BasePortletComposer {
	private static final long serialVersionUID = 1035332033957721736L;

	// 毕业证书照片
	// private static final String DA = "diploma";
	// 学信网学历证书
	private static final String EN = "Education";

	// 最高学位证书
	// private static final String TE = "topDegree";
	// 学信网学位证书
	private static final String DE = "degree";

	private UecOutDocEduApprovalViewMainBean bean = new UecOutDocEduApprovalViewMainBean();

	private UecDocEduManager uecDocEduManager = (UecDocEduManager) ApplicationContextUtil
			.getBean("uecDocEduManager");

	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");

	private UecOutStaffManager uecOutStaffManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");

	private ActRunTaskManager actRunTaskManager = (ActRunTaskManager) ApplicationContextUtil
			.getBean("actRunTaskManager");

	private String taskDefKey;

	private String taskId;

	private AtiOaBaseObject oaBaseObject;

	/**
	 * 外包人员基本信息对象
	 */
	private UecOutStaffInfo outStaffInfo = UecOutStaffInfo.newInstance();

	/**
	 * 人员教育经历
	 */
	private UecDocEdu uecDocEdu = UecDocEdu.newInstance();

	/**
	 * 人员教育经历历史
	 */
	private UecDocEdu uecDocEduHis = UecDocEdu.newInstance();

	/**
	 * 最高学历毕业证书:高校毕业证书照片
	 */
	// private Media certMedia;

	/**
	 * 最高学历毕业证书:学信网学历照片
	 */
	private Media fiveMedia;

	/**
	 * 最高学位证书:最高学位证书
	 */
	// private Media storeMedia;
	/**
	 * 最高学位证书:学信网学位证书
	 */
	private Media dEMedia;

	private String opType;

	private String procDefKey;

	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 界面初始化
	 */
	public void onCreate$uecOutDocEduApprovalViewMainWin()
			throws ParseException {
		bindCombobox();
		bindBean();
		checkEducationAndDegreeIsNo();
	}

	/**
	 * 编辑加载页面是要判断学历、学位是否为无
	 */
	public void checkEducationAndDegreeIsNo() {
		String index = (String) bean.getEducation().getSelectedItem()
				.getValue();
		if ("1001A1100000000006AW".equals(index)) {
			bean.getEducationctifcode().setDisabled(true);
			bean.getGlbdef9().setDisabled(true);
			bean.getGlbdef5().setDisabled(true);
			bean.getLasteducation().setDisabled(true);
			bean.getGlbdef8().setDisabled(true);
			bean.getGlbdef3().setDisabled(true);
			bean.getMajortype().setDisabled(true);

			// 设置 毕业证书相关 不可见
			// bean.getDownloadCertButton().setVisible(false);
			// bean.getUploadCertButton().setVisible(false);
//			bean.getCertPic().setVisible(false);
			// 设置学信网学历证书不可见
			bean.getDownloadFiveButton().setVisible(false);
			bean.getUploadFiveButton().setVisible(false);
			bean.getFivePic().setVisible(false);
			// 清数据
			bean.getEducationctifcode().setValue(null);
			bean.getGlbdef9().setSelectedIndex(0);
			bean.getGlbdef5().setSelectedIndex(0);
			bean.getLasteducation().setSelectedIndex(0);
			bean.getGlbdef8().setSelectedIndex(0);
			bean.getGlbdef3().setValue(null);
			bean.getMajortype().setSelectedIndex(0);
		}
		String index1 = (String) bean.getPkDegree().getSelectedItem()
				.getValue();
		if ("1001A1100000000006B3".equals(index1)) {
			bean.getCertifcode().setDisabled(true);
			bean.getDegreedate().setDisabled(true);
			bean.getDegreeunit().setDisabled(true);
			bean.getGlbdef4().setDisabled(true);

			// 设置最高学位证书图片 不可见
			// bean.getDownloadStoreButton().setVisible(false);
			// bean.getUploadStoreButton().setVisible(false);
//			bean.getStorePic().setVisible(false);
			// 学信网学位证书
			bean.getDownloadDEButton().setVisible(false);
			bean.getUploadDEButton().setVisible(false);
			bean.getDEPic().setVisible(false);

			// 清理数据
			bean.getCertifcode().setValue(null);
			bean.getDegreedate().setValue(null);
			bean.getDegreeunit().setValue(null);
			bean.getGlbdef4().setSelectedIndex(0);
		}
	}

	/**
	 * 监听学历下拉选择事件
	 */
	public void onSelect$education() {
		String index = (String) bean.getEducation().getSelectedItem()
				.getValue();
		if ("1001A1100000000006AW".equals(index)) {
			bean.getEducationctifcode().setDisabled(true);
			bean.getGlbdef9().setDisabled(true);
			bean.getGlbdef5().setDisabled(true);
			bean.getLasteducation().setDisabled(true);
			bean.getGlbdef8().setDisabled(true);
			bean.getGlbdef3().setDisabled(true);
			bean.getMajortype().setDisabled(true);

			// 设置 毕业证书相关 不可见
			// bean.getDownloadCertButton().setVisible(false);
			// bean.getUploadCertButton().setVisible(false);
//			bean.getCertPic().setVisible(false);
			// 设置学信网学历证书不可见
			bean.getDownloadFiveButton().setVisible(false);
			bean.getUploadFiveButton().setVisible(false);
			bean.getFivePic().setVisible(false);

			// 清数据
			bean.getEducationctifcode().setValue(null);
			bean.getGlbdef9().setSelectedIndex(0);
			bean.getGlbdef5().setSelectedIndex(0);
			bean.getLasteducation().setSelectedIndex(0);
			bean.getGlbdef8().setSelectedIndex(0);
			bean.getGlbdef3().setValue(null);
			bean.getMajortype().setSelectedIndex(0);
		} else {
			bean.getEducationctifcode().setDisabled(false);
			bean.getGlbdef9().setDisabled(false);
			bean.getGlbdef5().setDisabled(false);
			bean.getLasteducation().setDisabled(false);
			bean.getGlbdef8().setDisabled(false);
			bean.getGlbdef3().setDisabled(false);
			bean.getMajortype().setDisabled(false);

			if ("modify".equals(taskDefKey)) {
				// 设置 毕业证书相关 上传可见
				// bean.getDownloadCertButton().setVisible(false);
				// bean.getUploadCertButton().setVisible(true);
//				bean.getCertPic().setVisible(true);
				// 设置学信网学历证书 上传可见
				// bean.getDownloadFiveButton().setVisible(false);
				bean.getUploadFiveButton().setVisible(true);
				bean.getFivePic().setVisible(true);
			} else {
				// 设置 毕业证书相关 下载可见
				// bean.getDownloadCertButton().setVisible(true);
				// 设置学信网学历证书 下载可见
				bean.getDownloadFiveButton().setVisible(true);
			}
		}
	}

	/**
	 * 监听学位下拉选择
	 */
	public void onSelect$pkDegree() {
		String index = (String) bean.getPkDegree().getSelectedItem().getValue();
		if ("1001A1100000000006B3".equals(index)) {
			bean.getCertifcode().setDisabled(true);
			bean.getDegreedate().setDisabled(true);
			bean.getDegreeunit().setDisabled(true);
			bean.getGlbdef4().setDisabled(true);
			// 设置最高学位证书图片 不可见
			// bean.getDownloadStoreButton().setVisible(false);
			// bean.getUploadStoreButton().setVisible(false);
//			bean.getStorePic().setVisible(false);
			// 学信网学位证书
			bean.getDownloadDEButton().setVisible(false);
			bean.getUploadDEButton().setVisible(false);
			bean.getDEPic().setVisible(false);
			// 清理数据
			bean.getCertifcode().setValue(null);
			bean.getDegreedate().setValue(null);
			bean.getDegreeunit().setValue(null);
			bean.getGlbdef4().setSelectedIndex(0);
		} else {
			bean.getCertifcode().setDisabled(false);
			bean.getDegreedate().setDisabled(false);
			bean.getDegreeunit().setDisabled(false);
			bean.getGlbdef4().setDisabled(false);

			if ("modify".equals(taskDefKey)) {
				// 设置最高学位证书图片 上传可见
				// bean.getDownloadStoreButton().setVisible(false);
				// bean.getUploadStoreButton().setVisible(true);
//				bean.getStorePic().setVisible(true);
				// 学信网学位证书
				// bean.getDownloadDEButton().setVisible(false);
				bean.getUploadDEButton().setVisible(true);
				bean.getDEPic().setVisible(true);
			} else {
				// 设置最高学位证书图片 下载可见
				// bean.getDownloadStoreButton().setVisible(false);
				// 学信网学位证书
				bean.getDownloadDEButton().setVisible(false);
			}

		}
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getDegreedate().getValue())) {
			// 学位授予日期
			uecDocEdu.setDegreedate(formatter.format(bean.getDegreedate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef3().getValue())) {
			// 取得学历时间
			uecDocEdu
					.setGlbdef3(formatter.format(bean.getGlbdef3().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			// 入学日期
			uecDocEdu.setBegindate(formatter.format(bean.getBegindate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
			// 毕业日期
			uecDocEdu
					.setEnddate(formatter.format(bean.getEnddate().getValue()));
		}
	}

	/**
	 * 重新申请
	 * 
	 * @throws InterruptedException
	 */
	public void onOk() throws InterruptedException {
		// 非查看历史工单
		if (!ApprovalConstants.OP_TYPE_VIEW.equals(opType)) {
			// 判断任务是否已被处理
			boolean isCompleted = ApprovalUtil.isTaskCompleted(oaBaseObject,
					taskId);
			if (isCompleted) {
				onCancel();
				Events.postEvent(ApprovalConstants.ON_REFRESH_QUERY_RESPONSE,
						this.self, null);
				ZkUtil.showError("任务不存在或已被处理完毕！", "错误提示");
				return;
			}
			// 页面控件校验
			String msg = this.checkUacStaffData();
			if (null != msg) {
				Messagebox.show(msg);
				return;
			}
			Messagebox.show("是否提交审批？", "输入确认提示", Messagebox.OK
					| Messagebox.CANCEL, Messagebox.QUESTION,
					new EventListener() {
						@Override
						public void onEvent(Event event) throws Exception {
							if (((Integer) event.getData()).intValue() == Messagebox.OK) {
								postApprovalData();
								onCancel();
							} else {
								return;
							}
						}
					});
		} else {
			this.onCancel();
		}

	}

	public void postApprovalData() throws InterruptedException, IOException {
		String currentName = ApprovalUtil.getCurrentName();
		if (StrUtil.isNullOrEmpty(currentName)) {
			ZkUtil.showError("当前登录用户已失效，请重新登录!", "错误提示");
			return;
		}
		// 更新uecDocEdu
		PubUtil.fillPoFromBean(bean, uecDocEdu);
		dateTransToString();
		uecDocEdu.setModifiedtime(DateUtil.getCurrenDate());
		uecDocEdu.setModifier(currentName);
		DefaultDaoFactory.getDefaultDao().updateObject(uecDocEdu);

		instantOfUecEdu(currentName);

		Events.postEvent("onOKResponse", this.self,
				ApprovalConstants.ON_OK_RESULT);
	}

	/**
	 * 审批同意
	 * 
	 * @throws InterruptedException
	 */
	public void onApproval() throws InterruptedException {
		Messagebox.show("是否提交审批？", "输入确认提示", Messagebox.OK | Messagebox.CANCEL,
				Messagebox.QUESTION, new EventListener() {
					@Override
					public void onEvent(Event event) throws Exception {
						if (((Integer) event.getData()).intValue() == Messagebox.OK) {
							approvalData();
						} else {
							return;
						}
					}
				});
	}

	public void approvalData() {
		Map<String, Object> map = new HashMap<String, Object>();
		String comment = this.getComment(taskDefKey);
		map.put("comment", comment);
		map.put("onApprovalResult", ApprovalConstants.ON_APPROVAL_RESULT);
		Events.postEvent("onApprovalResponse", this.self, map);
		this.onCancel();

	}

	/**
	 * 审批拒绝
	 */
	public void onReject() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("comment", this.getComment(taskDefKey));
		map.put("onRejectResult", ApprovalConstants.ON_REJECT_RESULT);
		Events.postEvent("onRejectResponse", this.self, map);
		this.onCancel();
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		this.bean.getUecOutDocEduApprovalViewMainWin().onClose();
	}

	/**
	 * 获取传值
	 * 
	 * @throws ParseException
	 * @throws WrongValueException
	 */
	private void bindBean() throws ParseException {
		String procDefName = (String) arg.get("procDefName");
		this.bean.getUecOutDocEduApprovalViewMainWin().setTitle(
				procDefName + "审批");

		taskId = (String) arg.get("taskId");
		opType = (String) arg.get("opType");
		procDefKey = (String) arg.get("procDefKey");

		// 获取图片信息
		// TODO

		oaBaseObject = (AtiOaBaseObject) arg.get("oaBaseObject");
		if (!StrUtil.isNullOrEmpty(oaBaseObject)) {
			// 获取审批意见
			PubUtil.fillBeanFromPo(oaBaseObject, bean);
		}
		if ("onView".equals(opType)) {
			// 查看历史工单
			setButtonValid(true, false, false);
			setTextValid(true, true, true);
			setFileButton(false, true);
			bean.getEducation().setDisabled(true);
			bean.getPkDegree().setDisabled(true);
		} else {
			taskDefKey = (String) arg.get("taskDefKey");
			if ("modify".equals(taskDefKey)) {
				setButtonValid(true, false, false);
				setTextValid(false, false, false);
				setFileButton(true, false);
			} else {
				setButtonValid(false, true, true);
				setFileButton(false, true);
				bean.getEducation().setDisabled(true);
				bean.getPkDegree().setDisabled(true);
				// 显示图片
//				showPic(fiveMedia, bean.getFivePic());
//				showPic(dEMedia, bean.getDEPic());
			}
			if ("deptLeaderAudit".equals(taskDefKey)) {
				this.bean.getSecondText().setRows(2);
				this.setTextValid(true, false, false);
				bean.getSecondText().setValue(null);
			}
			if ("hrAudit".equals(taskDefKey)) {
				this.bean.getThirdText().setRows(2);
				this.setTextValid(false, true, false);
				bean.getThirdText().setValue(null);
			}
			if ("hrLeaderAudit".equals(taskDefKey)) {
				this.bean.getFourthText().setRows(2);
				this.setTextValid(false, false, true);
				bean.getFourthText().setValue(null);
			}
		}

		if (null != oaBaseObject) {
			List<Map<String, Object>> list = actRunTaskManager
					.getRuTasks(oaBaseObject.getProcInstId());
			if (null == list) {
				ZkUtil.showError("未知错误!", "错误信息");
				return;
			}
			if (list.size() == 0) {
				if (ApprovalUtil.OUT_STAFF_EDU_EDIT.equals(procDefKey)) {
					String eduIdTmp = oaBaseObject.getEduId();
					oaBaseObject.setEduId(oaBaseObject.getEduIdHis());
					oaBaseObject.setEduIdHis(eduIdTmp);
				}
			}

			// 获取人员信息
			String outStaffId = oaBaseObject.getOutStaffId();
			outStaffInfo.setOutStaffId(Long.parseLong(outStaffId));
			outStaffInfo = uecOutStaffManager
					.findUecOutStaffInfoByOutStaffId(outStaffInfo);
			if (null != outStaffInfo) {
				PubUtil.fillBeanFromPo(outStaffInfo, bean);
				UecOrgOrgs newOrgOrgs = UecOrgOrgs.newInstance();
				newOrgOrgs.setPkOrg(outStaffInfo.getPkHrorg());
				newOrgOrgs = uecOrgOrgsManager
						.findUecOrgOrgsByUecOrgOrgs(newOrgOrgs);
				if (newOrgOrgs != null) {
					bean.getUecOrgName().setValue(newOrgOrgs.getName());
				}
			}

			if (ApprovalUtil.OUT_STAFF_EDU_ADD.equals(procDefKey)) {
				// 获取教育经历信息
				uecDocEdu.setEduId(Long.parseLong(oaBaseObject.getEduId()));
				uecDocEdu = uecDocEduManager.findUecDocEdu(uecDocEdu);
				PubUtil.fillBeanFromPo(uecDocEdu, bean);
				stringTransToDate();

				// 设置历史对比信息栏不可见
				bean.getGroupboxHis().setVisible(false);
			} else if (ApprovalUtil.OUT_STAFF_EDU_EDIT.equals(procDefKey)) {
				// 获取教育经历信息
				uecDocEdu.setEduId(Long.parseLong(oaBaseObject.getEduId()));
				uecDocEdu = uecDocEduManager.findUecDocEdu(uecDocEdu);
				PubUtil.fillBeanFromPo(uecDocEdu, bean);
				stringTransToDate();

				// 设置历史对比信息栏可见
				bean.getGroupboxHis().setVisible(true);

				// 获取教育经历历史信息
				String uecEduIdHis = oaBaseObject.getEduIdHis();
				uecDocEduHis.setEduId(Long.parseLong(uecEduIdHis));
				uecDocEduHis = uecDocEduManager.findUecDocEdu(uecDocEduHis);

				/**
				 * 历史信息写到页面控件
				 */
				hisStringTransToDate();
				bean.getSchoolHis().setValue(uecDocEduHis.getSchool());
				setListboxSelectValue(bean.getSchooltypeHis(),
						uecDocEduHis.getSchooltype());
				bean.getMajorHis().setValue(uecDocEduHis.getMajor());
				setListboxSelectValue(bean.getSchooltypeHis(),
						uecDocEduHis.getGlbdef1());
				setListboxSelectValue(bean.getStudymodeHis(),
						uecDocEduHis.getStudymode());
				bean.getEdusystemHis().setValue(uecDocEduHis.getEdusystem());
				bean.getGlbdef2His().setValue(uecDocEduHis.getGlbdef2());
				setListboxSelectValue(bean.getMajortypeHis(),
						uecDocEduHis.getMajortype());
				setListboxSelectValue(bean.getPkDegreeHis(),
						uecDocEduHis.getPkDegree());
				setListboxSelectValue(bean.getEducationHis(),
						uecDocEduHis.getEducation());
				bean.getCertifcodeHis().setValue(uecDocEduHis.getCertifcode());
				bean.getEducationctifcodeHis().setValue(
						uecDocEduHis.getEducationctifcode());
				bean.getDegreeunitHis().setValue(uecDocEduHis.getDegreeunit());
				setListboxSelectValue(bean.getGlbdef9His(),
						uecDocEduHis.getGlbdef9());
				setListboxSelectValue(bean.getGlbdef5His(),
						uecDocEduHis.getGlbdef5());
				setListboxSelectValue(bean.getLasteducationHis(),
						uecDocEduHis.getLasteducation());
				setListboxSelectValue(bean.getGlbdef4His(),
						uecDocEduHis.getGlbdef4());
				setListboxSelectValue(bean.getGlbdef8His(),
						uecDocEduHis.getGlbdef8());
			}
		}

	}

	@SuppressWarnings("unchecked")
	public void setListboxSelectValue(Listbox listbox, String selectValue) {
		List<Listitem> items = listbox.getChildren();
		for (Listitem listitem : items) {
			if (null != listitem && null != listitem.getValue()) {
				if (listitem.getValue().toString().equals(selectValue)) {
					listbox.setSelectedItem(listitem);
					break;
				}
			}
		}
	}

	/**
	 * 图片上传：高校毕业证书图片
	 * 
	 * @param event
	 * @throws Exception
	 */
	// public void onUpload$uploadCertButton(ForwardEvent event) throws
	// Exception {
	// certMedia = ((UploadEvent) event.getOrigin()).getMedia();
	//
	// if (null == certMedia) {
	// ZkUtil.showError("请选择要上传的图片!", "系统提示");
	// return;
	// } else if (!(certMedia.getName().endsWith(".jpg"))) {
	// certMedia = null;
	// ZkUtil.showError("上传的图片不是jpg类型!", "系统提示");
	// return;
	// }
	//
	// showPic(certMedia, bean.getCertPic());
	// }

	/**
	 * 下载图片 毕业证书
	 * 
	 * @throws SQLException
	 * @throws IOException
	 */
	// public void onDownloadCert() throws SQLException, IOException {
	// onDownloadPic(uecDocEdu, DA);
	// }

	/**
	 * 图片上传：学信网学历证书
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onUpload$uploadFiveButton(ForwardEvent event) throws Exception {
		fiveMedia = ((UploadEvent) event.getOrigin()).getMedia();

		if (null == fiveMedia) {
			ZkUtil.showError("请选择要上传的图片!", "系统提示");
			return;
		} else if (!(fiveMedia.getName().endsWith(".jpg"))) {
			fiveMedia = null;
			ZkUtil.showError("上传的图片不是jpg类型!", "系统提示");
			return;
		}

		showPic(fiveMedia, bean.getFivePic());
	}

	/**
	 * 下载图片 学信网学历证书
	 * 
	 * @throws SQLException
	 * @throws IOException
	 */
	public void onDownloadFive() throws SQLException, IOException {
		onDownloadPic(uecDocEdu, EN);
	}

	/**
	 * 图片上传：最高学位证书
	 * 
	 * @param event
	 * @throws Exception
	 */
	// public void onUpload$uploadStoreButton(ForwardEvent event) throws
	// Exception {
	// storeMedia = ((UploadEvent) event.getOrigin()).getMedia();
	//
	// if (null == storeMedia) {
	// ZkUtil.showError("请选择要上传的图片!", "系统提示");
	// return;
	// } else if (!(storeMedia.getName().endsWith(".jpg"))) {
	// storeMedia = null;
	// ZkUtil.showError("上传的图片不是jpg类型!", "系统提示");
	// return;
	// }
	//
	// showPic(storeMedia, bean.getStorePic());
	// }

	/**
	 * 下载图片 最高学位证书
	 * 
	 * @throws SQLException
	 * @throws IOException
	 */
	// public void onDownloadStore() throws SQLException, IOException {
	// onDownloadPic(uecDocEdu, TE);
	// }

	/**
	 * 图片上传：学信网学位证书图片
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onUpload$uploadDEButton(ForwardEvent event) throws Exception {
		dEMedia = ((UploadEvent) event.getOrigin()).getMedia();

		if (null == dEMedia) {
			ZkUtil.showError("请选择要上传的图片!", "系统提示");
			return;
		} else if (!(dEMedia.getName().endsWith(".jpg"))) {
			dEMedia = null;
			ZkUtil.showError("上传的图片不是jpg类型!", "系统提示");
			return;
		}

		showPic(dEMedia, bean.getDEPic());
	}

	/**
	 * 下载图片 学信网学位证书
	 * 
	 * @throws SQLException
	 * @throws IOException
	 */
	public void onDownloadDE() throws SQLException, IOException {
		onDownloadPic(uecDocEdu, DE);
	}

	/**
	 * 设置图片显示的宽高
	 * 
	 * @param picMedia
	 * @param picImage
	 */
	public void showPic(Media picMedia, org.zkoss.zul.Image picImage) {
		Image img = (Image) picMedia;
		picImage.setHeight("30px");
		picImage.setWidth("30px");

		picImage.setContent(img);
	}

//	public void downloadMedia(UecDocEdu uecDocEdu, String picType) {
//		UecOutStaffInfo uec = new UecOutStaffInfo();
//		uec.setOutStaffId(uecDocEdu.getStaffId());
//		UecOutStaffInfo outStaff = uecOutStaffManager
//				.findUecOutStaffInfoByOutStaffId(uec);
//		if (null != outStaff) {
//			PictureTable picture = uecOutStaffManager.getPicture(
//					outStaff.getPsnCode(), picType);
//			if (null != picture) {
//				// 获取图片
//				/**
//				 * Blob字段的所有操作必须在事务中进行，否则只保存对象的引用，走出事务就失效了
//				 */
//				byte[] bs = uecOutStaffManager.getPictureInputStream(
//						outStaff.getPsnCode(), picType);
//				if (null == bs) {
//					fiveMedia = null;
//				}
//				fiveMedia.getByteData()
//			} else {
//				ZkUtil.showError("下载图片失败,图片不存在", "系统提示");
//			}
//		}
//	}

	/**
	 * 下载图片 根据图片类型flag
	 * 
	 * @throws SQLException
	 * @throws IOException
	 */
	public void onDownloadPic(UecDocEdu uecDocEdu, String picType)
			throws SQLException, IOException {
		try {
			UecOutStaffInfo uec = new UecOutStaffInfo();
			uec.setOutStaffId(uecDocEdu.getStaffId());
			UecOutStaffInfo outStaff = uecOutStaffManager
					.findUecOutStaffInfoByOutStaffId(uec);
			if (null != outStaff) {
				PictureTable picture = uecOutStaffManager.getPicture(
						outStaff.getPsnCode(), picType);
				if (null != picture) {
					String charset = "UTF-8";
					// 图片名称
					String fileName = picture.getPicName();
					// 编码后文件名
					String encodedName = null;
					encodedName = URLEncoder.encode(fileName, charset);
					// 将空格替换为+号
					encodedName = encodedName.replace("%20", "+");
					HttpServletRequest httpRequest = (HttpServletRequest) Executions
							.getCurrent().getNativeRequest();
					// 解决ie6 bug 或者是火狐浏览器
					if (encodedName.length() > 150
							|| Servlets.isGecko(httpRequest)
							|| Servlets.isGecko3(httpRequest)) {
						encodedName = new String(fileName.getBytes(charset),
								"ISO8859-1");
					}
					// 获取图片
					/**
					 * Blob字段的所有操作必须在事务中进行，否则只保存对象的引用，走出事务就失效了
					 */
					byte[] bs = uecOutStaffManager.getPictureInputStream(
							outStaff.getPsnCode(), picType);
					if (null != bs) {
						Filedownload.save(bs, "application/x-jpg", encodedName);
					} else {
						ZkUtil.showError("下载图片失败,图片不存在", "系统提示");
					}
				} else {
					ZkUtil.showError("下载图片失败,图片不存在", "系统提示");
				}
			}
		} catch (UnsupportedEncodingException e) {
			ZkUtil.showError("下载图片失败，文件名不支持", "系统提示");
		}
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 学位授予日期
		bean.getDegreedate().setValue(
				DateUtil.patternStrToDate(uecDocEdu.getDegreedate()));
		// 取得学历时间
		bean.getGlbdef3().setValue(
				DateUtil.patternStrToDate(uecDocEdu.getGlbdef3()));
		// 入学日期
		bean.getBegindate().setValue(
				DateUtil.patternStrToDate(uecDocEdu.getBegindate()));
		// 毕业日期
		bean.getEnddate().setValue(
				DateUtil.patternStrToDate(uecDocEdu.getEnddate()));
	}

	/**
	 * 将历史数据String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void hisStringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 学位授予日期
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		bean.getDegreedateHis().setValue(uecDocEduHis.getDegreedate());
		// 取得学历时间
		bean.getGlbdef3His().setValue(uecDocEduHis.getGlbdef3());
		// 入学日期
		bean.getBegindateHis().setValue(uecDocEduHis.getBegindate());
		// 毕业日期
		bean.getEnddateHis().setValue(uecDocEduHis.getEnddate());

	}

	/**
	 * 获取审批意见
	 * 
	 * @param taskDefKey
	 * @return
	 */
	private String getComment(String taskDefKey) {
		if ("deptLeaderAudit".equals(taskDefKey)) {
			return this.bean.getSecondText().getValue();
		}
		if ("hrAudit".equals(taskDefKey)) {
			return this.bean.getThirdText().getValue();
		}
		if ("hrLeaderAudit".equals(taskDefKey)) {
			return this.bean.getFourthText().getValue();
		}
		return null;
	}

	/**
	 * 添加一个uecEdu对象
	 * 
	 * @param currentName
	 * @throws IOException
	 * @throws InterruptedException
	 */
	private void instantOfUecEdu(String currentName) throws IOException,
			InterruptedException {
		UecDocEdu uecDocEdu = UecDocEdu.newInstance();
		PubUtil.fillPoFromBean(bean, uecDocEdu);
		dateTransToString();
		uecDocEdu.setStaffId(outStaffInfo.getOutStaffId());
		uecDocEdu.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
		uecDocEdu.setPkPsndoc(outStaffInfo.getPkPsnMsg());
		uecDocEdu.setCreator(currentName);
		uecDocEdu.setCreationtime(DateUtil.getCurrenDate());
		// 上传相关图片
		if (/* null != certMedia && */null != fiveMedia) {
			// saveMedia(certMedia, outStaffInfo, DA);
			saveMedia(fiveMedia, outStaffInfo, EN);
		}
		// 上传学位照片
		if (/* null != storeMedia && */null != dEMedia) {
			// saveMedia(storeMedia, outStaffInfo, TE);
			saveMedia(dEMedia, outStaffInfo, DE);
		}
	}

	/**
	 * 保存图片 将图片转二进制保存在数据库中 注意：一个人员只能上传四张照片：毕业证书照片、学信网学历照片、最高学位证书、学信网学位证书图片
	 * 根据这个人的员工编码和照片类型来确定唯一值，如果重复则update;否则add
	 * 
	 * @param picMedia
	 * @throws IOException
	 */
	public void saveMedia(Media picMedia, UecOutStaffInfo uecOutStaffInfo,
			String picType) throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(
				picMedia.getByteData());
		@SuppressWarnings("deprecation")
		Blob blob = Hibernate.createBlob(bais);
		PictureTable pic = new PictureTable();
		pic.setStaffCode(uecOutStaffInfo.getPsnCode());
		pic.setPicType(picType);
		pic = uecOutStaffManager.isExitPictureTable(pic);
		if (null == pic) {
			pic = PictureTable.newInstance();
			pic.setStaffCode(uecOutStaffInfo.getPsnCode());
			pic.setPicType(picType);
			pic.setPicName(picMedia.getName());
			pic.setPicture(blob);
			pic.addOnly();
		} else {
			pic.setPicName(picMedia.getName());
			pic.setPicture(blob);
			pic.updateOnly();
		}
	}

	/**
	 * 设置文件上传和下载按钮
	 * 
	 * @param canUpload
	 * @param canDownload
	 */
	private void setFileButton(boolean canUpload, boolean canDownload) {
		// bean.getUploadCertButton().setVisible(canUpload);
		// bean.getUploadCertButton().setDisabled(!canUpload);
		bean.getUploadFiveButton().setVisible(canUpload);
		bean.getUploadFiveButton().setDisabled(!canUpload);
		// bean.getUploadStoreButton().setVisible(canUpload);
		// bean.getUploadStoreButton().setDisabled(!canUpload);
		bean.getUploadDEButton().setVisible(canUpload);
		bean.getUploadDEButton().setDisabled(!canUpload);
		// bean.getDownloadCertButton().setVisible(canDownload);
		// bean.getDownloadCertButton().setDisabled(!canDownload);
		bean.getDownloadFiveButton().setVisible(canDownload);
		bean.getDownloadFiveButton().setDisabled(!canDownload);
		// bean.getDownloadStoreButton().setVisible(canDownload);
		// bean.getDownloadStoreButton().setDisabled(!canDownload);
		bean.getDownloadDEButton().setVisible(canDownload);
		bean.getDownloadDEButton().setDisabled(!canDownload);

	}

	/**
	 * 设置审批按钮
	 * 
	 * @param canEdit
	 * @param canApproval
	 * @param canReject
	 */
	private void setButtonValid(boolean canEdit, boolean canApproval,
			boolean canReject) {
		this.bean.getOkBtn().setVisible(canEdit);
		this.bean.getOkBtn().setDisabled(!canEdit);

		this.bean.getApprovalBtn().setVisible(canApproval);
		this.bean.getApprovalBtn().setDisabled(!canApproval);

		this.bean.getRejectBtn().setVisible(canReject);
		this.bean.getRejectBtn().setDisabled(!canReject);

	}

	/**
	 * 设置审批意见栏
	 * 
	 * @param secondCanEdit
	 * @param thirdCanEdit
	 * @param fourthCanEdit
	 */
	private void setTextValid(boolean secondCanEdit, boolean thirdCanEdit,
			boolean fourthCanEdit) {
		this.bean.getSecondText().setDisabled(!secondCanEdit);
		this.bean.getThirdText().setDisabled(!thirdCanEdit);
		this.bean.getFourthText().setDisabled(!fourthCanEdit);
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getSchool().getValue())) {
			return "请填写学校名称";
		} else if (StrUtil.isNullOrEmpty(bean.getGlbdef1().getSelectedItem()
				.getValue())) {
			return "请选择学校类别";
		} else if (StrUtil.isNullOrEmpty(bean.getStudymode().getSelectedItem()
				.getValue())) {
			return "请选择学习类型";
		} else if (StrUtil.isNullOrEmpty(bean.getEdusystem().getValue())) {
			return "请填写学制";
		} else if (StrUtil.isNullOrEmpty(bean.getGlbdef2().getValue())) {
			return "请填写所在院系";
		} else if (StrUtil.isNullOrEmpty(bean.getMajor().getValue())) {
			return "请填写专业";
		}
		if (!StrUtil.isNullOrEmpty(bean.getEducation().getSelectedItem()
				.getValue())) {
			if (!"1001A1100000000006AW".equals(bean.getEducation()
					.getSelectedItem().getValue())) {
				if (StrUtil.isNullOrEmpty(bean.getMajortype().getSelectedItem()
						.getValue())) {
					return "请选择学历专业类别";
				} else if (StrUtil.isNullOrEmpty(bean.getGlbdef3().getValue())) {
					return "请选择取得学历时间";
				} else if (StrUtil.isNullOrEmpty(bean.getEducationctifcode()
						.getValue())) {
					return "请填写学历证书编号";
				} else if (StrUtil.isNullOrEmpty(bean.getGlbdef5()
						.getSelectedItem().getValue())) {
					return "请选择是否第一学历";
				} else if (StrUtil.isNullOrEmpty(bean.getGlbdef8()
						.getSelectedItem().getValue())) {
					return "请选择是否全日制最高学历";
				} else if (StrUtil.isNullOrEmpty(bean.getLasteducation()
						.getSelectedItem().getValue())) {
					return "请选择是否最高学历";
				} else if (StrUtil.isNullOrEmpty(bean.getGlbdef9()
						.getSelectedItem().getValue())) {
					return "请选择是否在职最高学历";
				} /*
				 * else if (null == certMedia) { return "请上传  高校毕业证书"; } else if
				 * (null == fiveMedia) { return "学信网学历证书图片"; }
				 */
			}
		}
		if (!StrUtil.isNullOrEmpty(bean.getPkDegree().getSelectedItem()
				.getValue())) {
			if (!"1001A1100000000006B3".equals(bean.getPkDegree()
					.getSelectedItem().getValue())) {
				if (StrUtil.isNullOrEmpty(bean.getGlbdef4().getSelectedItem()
						.getValue())) {
					return "请选择是否最高学位";
				} else if (StrUtil.isNullOrEmpty(bean.getDegreedate()
						.getValue())) {
					return "请填写学位授予日期";
				} else if (StrUtil.isNullOrEmpty(bean.getCertifcode()
						.getValue())) {
					return "请填写学位证书编号";
				} else if (StrUtil.isNullOrEmpty(bean.getDegreeunit()
						.getValue())) {
					return "请填写学位授予单位";
				}
				if ("Y".equals(bean.getGlbdef4().getSelectedItem().getValue())) {
					/*
					 * if(null == storeMedia) { return "请上传 最高学位证书"; }else
					 */if (null == dEMedia) {
						return "学信网学位证书图片";
					}
				}
			}
		}
		return null;
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		this.bindComboboxBean();
	}

	/**
	 * 未采用数据字典，这里是暂时的转换,将枚举量翻译
	 */
	public void bindComboboxBean() {
		// 学校类别
		ListboxUtils.rendererForEdit(bean.getGlbdef1(),
				NodeVoUtil.getDictionaries(UecConstants.SCHOOL_TYPE));
		// 历史 学校类别
		ListboxUtils.rendererForEdit(bean.getSchooltypeHis(),
				NodeVoUtil.getDictionaries(UecConstants.SCHOOL_TYPE));
		// 学习方式studymode
		ListboxUtils.rendererForEdit(bean.getStudymode(),
				NodeVoUtil.getDictionaries(UecConstants.STUDY_MODE));
		// 历史 学习方式
		ListboxUtils.rendererForEdit(bean.getStudymodeHis(),
				NodeVoUtil.getDictionaries(UecConstants.STUDY_MODE));
		// 学历education
		ListboxUtils.rendererForEdit(bean.getEducation(),
				NodeVoUtil.getDictionaries(UecConstants.EDUCATION));
		// 历史 学历
		ListboxUtils.rendererForEdit(bean.getEducationHis(),
				NodeVoUtil.getDictionaries(UecConstants.EDUCATION));
		// 学位pkDegree
		ListboxUtils.rendererForEdit(bean.getPkDegree(),
				NodeVoUtil.getDictionaries(UecConstants.ACADEMIC_DEGREE));
		// 历史 学位
		ListboxUtils.rendererForEdit(bean.getPkDegreeHis(),
				NodeVoUtil.getDictionaries(UecConstants.ACADEMIC_DEGREE));
		// 学历专业类别majortype
		ListboxUtils.rendererForEdit(bean.getMajortype(),
				NodeVoUtil.getDictionaries(UecConstants.MAJOR_TYPE));
		// 历史学历专业类别majortype
		ListboxUtils.rendererForEdit(bean.getMajortypeHis(),
				NodeVoUtil.getDictionaries(UecConstants.MAJOR_TYPE));

		// 是否第一学历
		List<NodeVo> nodeVoIshiskeypsnList = new ArrayList<NodeVo>();
		String[][] ishiskeypsn = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < ishiskeypsn.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn[i][j]);
				nodeVo.setName(ishiskeypsn[i][j + 1]);
			}
			nodeVoIshiskeypsnList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef5(), nodeVoIshiskeypsnList);
		// 历史
		ListboxUtils.rendererForEdit(bean.getGlbdef5His(),
				nodeVoIshiskeypsnList);

		// 是否最高学历
		ListboxUtils.rendererForEdit(bean.getLasteducation(),
				nodeVoIshiskeypsnList);
		// 历史
		ListboxUtils.rendererForEdit(bean.getLasteducationHis(),
				nodeVoIshiskeypsnList);

		// 是否最高学位
		ListboxUtils.rendererForEdit(bean.getGlbdef4(), nodeVoIshiskeypsnList);
		// 历史
		ListboxUtils.rendererForEdit(bean.getGlbdef4His(),
				nodeVoIshiskeypsnList);

		// 是否全日制最高学历
		ListboxUtils.rendererForEdit(bean.getGlbdef8(), nodeVoIshiskeypsnList);
		// 历史
		ListboxUtils.rendererForEdit(bean.getGlbdef8His(),
				nodeVoIshiskeypsnList);

		// 是否在职最高学历
		ListboxUtils.rendererForEdit(bean.getGlbdef9(), nodeVoIshiskeypsnList);
		// 历史
		ListboxUtils.rendererForEdit(bean.getGlbdef9His(),
				nodeVoIshiskeypsnList);

	}
}
